# url：http://www.bjhospital.net/
# 用例1：
# 搜索骨科—>点击搜索—>点击出来的第一个链接—>断言文本内容
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class Search:
    def __init__(self,driver):
        self.driver = driver
        self.driver.get("http://www.bjhospital.net/")
        driver.maximize_window()
        self.driver.implicitly_wait(15)
    def input_text(self,text:str):
        input = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.NAME,"q")))
        input.clear() # 先清除
        input.send_keys(text)
    def click_search_button(self):
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,'btn')))[0].click()
    def click_a(self):
        class_resu = self.driver.find_element(By.CLASS_NAME,"resultWrap") # 先定位到父级别
        ul = class_resu.find_element(By.TAG_NAME,"ul")
        li = ul.find_elements(By.TAG_NAME,'li')[0]
        li.find_elements(By.TAG_NAME,'a')[0].click()


    def switch_win(self):
        driver.switch_to.window(driver.window_handles[-1])
    def assert_text(self):
        container_inner = self.driver.find_element(By.CLASS_NAME,"container-inner")
        content = container_inner.find_element(By.CLASS_NAME,'content')
        content = content.find_element(By.TAG_NAME,'h1').text
        assert content == "毕节市第一人民医院高值医用耗材网上阳光平台议价意向公开"
    def close(self):
        self.driver.quit()


if __name__ == '__main__':
    driver = webdriver.Chrome()
    d1 = Search(driver)
    d1.input_text("骨科")
    d1.click_search_button()
    d1.click_a()
    d1.switch_win()
    d1.assert_text()
    d1.close()





